/**
 *    @file       udpsendthread.cpp
 *
 *    @brief      
 *    @details
 *
 *    @version    1.0
 *    @date       02/19/2014 12:22:38 PM
 *
 *    @author     Theophilus (), wangtf418@gmail.com
 */
#include "udpsendthread.h"

udpsendthread::udpsendthread(bf_uint32_t type, bf_uint32_t size, bf_uint32_t index, const comargs *args)
	: synworkthread(type, size, index, args)
{

}

udpsendthread::~udpsendthread()
{

}

bf_int_t udpsendthread::init()
{
	srv_addr_args *args = (srv_addr_args*)m_args;
	if (!args)
	{
		return BF_ERROR;
	}
	bf_int_t ret = m_socket.socket(SOCK_DGRAM);
	if (ret != BF_OK)
	{
		return BF_ERROR;
	}

	m_socket.set_reuse_opt(true);
	ret = m_socket.bind();
	if (ret != BF_OK)
	{
		return BF_ERROR;
	}

	bf_int_t socket_buf_len = 8388608; //8MB
	m_socket.set_recvbuf_opt(socket_buf_len);
	m_socket.set_sendbuf_opt(socket_buf_len);

	return BF_OK;
}

bf_int_t udpsendthread::proc_msg(message *msg)
{
	addr_bufmsgbody *body = (addr_bufmsgbody*)msg->get_msgbody();
	if (body && body->get_buf() && body->get_buflen() > 0)
	{
		m_socket.sendto(body->get_buf(), body->get_buflen(), body->get_addr());
	}
	return BF_OK;
}

